package com.base.leetcode;

/**
 * @Author: hdhao
 * @Create: 2023/3/30 - 22:52
 * @Version: v1.0
 */
//检测链表是否有环
public class Leetcode141 {

    //使用龟兔赛跑算法(快慢指针)
    public boolean hasCycle(ListNode head){
        //兔子指针(每次走两步)
        ListNode h = head;
        //乌龟指针(每次走一步)
        ListNode t = head;
        while (h!=null && h.next!=null){
            t = t.next;
            h = h.next.next;
            if (t == h){
                return true;
            }
        }
        return false;
    }
}
